#!/usr/bin/env groovy
// @file    ScanController
// @author  Mac Radigan
// @version $Id: ScanSystem.groovy 30 2011-04-10 04:12:24Z mac.radigan $

package ycoa.scan.process

import ycoa.scan.signin.GravicSigninProcessor
import ycoa.archive.Archive
import ycoa.configuration.YCOAConfiguration
import ycoa.scan.barcode.GravicBarcodeProcessor

import org.apache.log4j.Logger

public class ScanController {

  private static ScanController ref = null
  private boolean lock = false
  private config = YCOAConfiguration.getConfiguration()

  static Logger log = Logger.getLogger(ScanController.class)

  public static ScanController getInstance() {
    if(null==ref) {
      ref = new ScanController()
    }
    return ref
  }

  private ScanController() { /* singleton */ }

  private String process() {
    log.debug("scan controller processing...")
    if(lock) throw new Exception("Scan processor in use, and the processing resource is locked.")
    try {
      log.debug("scan controller lock enable")
      lock = true
      def private processor = null
      config.server.eachWithIndex() { host, index ->
        log.debug("processing ${host}")
	// perform signin processing
        processor = new GravicSigninProcessor(host.key)
        processor.allInOne()
	// perform barcode processing
        //processor = new GravicBarcodeProcessor(host.key)
        //processor.allInOne()
      }
      log.debug("scan controller lock release")
      lock = false
      return "PROCESS RESULT:  ${processor.getMessage()}"
    } catch(e) {
      lock = false
      log.error("Error in ScanController.  ${e.getMessage()}")
      return "testing..."
      //return e.getMessage()
      throw new Exception("Error in ScanController.", e)
    }
  }

}

// *EOF*
